package com.xinzhi.seckill.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinzhi.seckill.exception.GlobalException;
import com.xinzhi.seckill.mapper.UserMapper;
import com.xinzhi.seckill.pojo.User;
import com.xinzhi.seckill.service.IUserService;
import com.xinzhi.seckill.utils.CookieUtils;
import com.xinzhi.seckill.utils.MD5Util;
import com.xinzhi.seckill.utils.UUIDUtils;
import com.xinzhi.seckill.vo.LoginVo;
import com.xinzhi.seckill.vo.RespBean;
import com.xinzhi.seckill.vo.RespBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zanrihui
 * @since 2022-01-06
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 登录实现类
     * @param loginVo
     * @param request
     * @param response
     * @return
     */
    @Override
    public RespBean doLogin(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
//        //判断用户名，参数校验
//        if (StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)){
//            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
//        }
//        if(!ValidatorUtil.isMobile(mobile)){
//            return RespBean.error(RespBeanEnum.MOBILE_ERROR);
//        }
        User user = userMapper.selectById(mobile);
        //判断是否能查到用户,根据手机号获取用户
        if (null== user){
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }
        //如果用户不为空，就去校验密码
        if (!MD5Util.formPassToDBPass(password,user.getSlat()).equals(user.getPassword())){
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }

        //生成cookie
        String ticket = UUIDUtils.uuid();
        //将用户信息存入redis中
        //opsForValue主要操作String类型
        redisTemplate.opsForValue().set("user:"+ticket,user);
        //k是cookie  v是user对象
//        request.getSession().setAttribute(ticket,user);
        //生成cookie,将ticket放到对应的cookie中
        CookieUtils.setCookie(request,response,"userTicket",ticket);
        return RespBean.success();
    }

    /**
     * 根据cookie获取用户
     * @param userTicket
     * @return
     */
    @Override
    public User getUserByCookie(String userTicket,HttpServletRequest request,HttpServletResponse response) {
        if (StringUtils.isEmpty(userTicket)){
            return null;
        }
        User user = (User) redisTemplate.opsForValue().get("user:" + userTicket);
        if (user!=null){
            CookieUtils.setCookie(request,response,"userTicket",userTicket);

        }        return user;
    }
}
